<template>
	<el-dialog :visible="showDialog" @close="btnCancel" :title="showTitle">
		<el-form ref="deptForm" :model="formData" :rules="rules" label-width="100px">
			<el-form-item prop="name" label="部门名称">
				<el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符"></el-input>
			</el-form-item>
			<el-form-item prop="code" label="部门编码">
				<el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
			</el-form-item>
			<el-form-item prop="manager" label="部门负责人">
				<el-select v-model="formData.manager" style="width:80%" @focus="getEmployeeSimple" placeholder="请选择">
					<el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
				</el-select>
			</el-form-item>
			<el-form-item prop="introduce" label="部门介绍">
				<el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea"
					:rows="3" />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-row type="flex" justify="center">
				<el-col :span="6">
					<el-button @click="btnOK" type="primary" size="small">确定</el-button>
					<el-button @click="btnCancel" size="small">取消</el-button>
				</el-col>
			</el-row>
		</template>
	</el-dialog>
</template>

<script>
	import {
		getDepartments,
		addDepartments,
		getDepartDetail,
		updateDepartments
	} from '@/api/departments.js'
	import {
		getEmployeeSimple
	} from '@/api/employees.js'

	export default {
		props: {
			showDialog: {
				type: Boolean,
				default: false
			},
			treeNode: {
				type: Object,
				default: null
			}
		},

		data() {
			const checkNameRepeat = async (rule, value, callback) => {
				const {
					depts
				} = await getDepartments()
				let isRepeat = false

				if (this.formData.id) {
					// 如果是修改 treeNode代表子，treeNode.pid代表父
					// 如果父级相等，且子id不等，且value相同
					
					isRepeat = depts.filter(item => item.pid === this.treeNode.pid && item.id !== this.treeNode.id)
						.some(item => item.name === value)
				} else {
					// 如果是新增， treeNode代表父
					// 如果父相等，且value相同
					isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name ===
						value)
				}
				isRepeat ? callback(new Error(`同级部门下已经有${value}了`)) : callback()
			}

			const checkCodeRepeat = async (rule, value, callback) => {
				// 先要获取最新的组织架构数据
				const {
					depts
				} = await getDepartments()
				let isRepeat = false

				if (this.formData.id) {
					// 如果是修改,如果id不相等，但是code相同，则重复
					isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
				} else {
					// 如果是新增
					isRepeat = depts.some(item => item.code === value &&
						value) // 这里加一个 value不为空 因为我们的部门有可能没有code
				}
				isRepeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
			}

			return {
				formData: {
					name: '', // 部门名称
					code: '', // 部门编码
					manager: '', // 部门管理者
					introduce: '' // 部门介绍
				},
				rules: {
					name: [{
							required: true,
							message: '部门名称不能为空',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 50,
							message: '部门名称要求1-50个字符',
							trigger: 'blur'
						}, {
							trigger: 'blur',
							validator: checkNameRepeat
						}
					],
					code: [{
							required: true,
							message: '部门编码不能为空',
							trigger: 'blur'
						},
						{
							min: 1,
							max: 50,
							message: '部门编码要求1-50个字符',
							trigger: 'blur'
						}, {
							trigger: 'blur',
							validator: checkCodeRepeat
						}
					],
					manager: [{
						required: true,
						message: '部门负责人不能为空',
						trigger: 'blur'
					}],
					introduce: [{
							required: true,
							message: '部门介绍不能为空',
							trigger: 'blur'
						},
						{
							trigger: 'blur',
							min: 1,
							max: 300,
							message: '部门介绍要求1-50个字符'
						}
					]
				},
				peoples: [],
			}
		},

		methods: {
			async getEmployeeSimple() {
				this.peoples = await getEmployeeSimple()
			},

			btnOK() {
				this.$refs.deptForm.validate(async isOK => {
					if (isOK) {
						if (this.formData.id) {
							await updateDepartments(this.formData)
						} else {
							await addDepartments({
								...this.formData,
								pid: this.treeNode.id
							})
						}
						this.$emit('addDepts')
						this.$emit('update:showDialog', false)
					}
				})
			},

			btnCancel() {
				this.formData = {
					name: '',
					code: '',
					manager: '',
					introduce: ''
				}
				this.$refs.deptForm.resetFields()
				this.$emit('update:showDialog', false)
			},

			async getDepartDetail(id) {
				this.formData = await getDepartDetail(id)
			}
		},

		computed: {
			showTitle() {
				return this.formData.id ? '编辑部门' : '添加子部门'
			}
		},
	}
</script>

<style scoped>

</style>
